<?php
/*
 * 会员登录
 * @arrange Mark
 * @package ixdcw
 * @file    login.inc.php
 * @time    12-12-19 上午11:47
 */
defined('IN_IXDCW') or exit('Access Denied');
if($_userid) dheader($MOD['linkurl']);
require DT_CORE.'/'.$module.'/common.inc.php';
require_once MD_ROOT.'/member.class.php';
require_once DT_INC.'/post.func.php';
$do = new member;
// 是否设置了转向
$forward = $forward ? linkurl($forward, 1) : SITE_URL;
//if(isset($auto)) $submit = true;

$action = 'login';
if($_POST) {
	captcha($captcha, $MOD['captcha_login']);
	$username = trim($username);
	if(!$username){
		$username = trim($lusername);
	}
	$password = trim($password);
	if(!$password){
		$password = trim($lpassword);
	}
	if(strlen($username) < 1) Prompt::message($L['login_msg_username']);
	if(strlen($password) < 1) Prompt::message($L['login_msg_password']);
	// 设置了转向到会员中心
	$goto = isset($goto) ? true : false;
	if($goto) $forward = $MOD['linkurl'];
	$cookietime = isset($cookietime) ? $cookietime : 0;
	$api_msg = $api_url = '';
	// 登陆类型
	$option = isset($option) ? trim($option) : 'username';
	if(is_email($username) && $option == 'username') $option = 'email';
	if(!User::checkname($username) && $option == 'username') $option = 'passport';
	in_array($option, array('username', 'passport', 'email', 'mobile', 'company', 'userid')) or $option = 'username';

	$r = $db->getOne("SELECT username,passport FROM {$db->getPre()}member WHERE `$option`='$username'");
	if($r) {
		$username = $r['username'];
		$passport = $r['passport'];
	} else {
		if($option == 'username' || $option == 'passport') {
			$passport = $username;
			// $MOD['passport'] 启用会员整合
			if($option == 'username' && $MOD['passport']) {
				$r = $db->getOne("SELECT username FROM {$db->getPre()}member WHERE `passport`='$username'");
				if($r) $username = $r['username'];
			}
		} else {
			Prompt::message($L['login_msg_not_member'].'?');
		}
	}

	if($MOD['passport'] == 'uc') include DT_ROOT.'/api/'.$MOD['passport'].'.inc.php';
	$user = $do->login($username, $password, $cookietime);
	if($user) {
		if($MOD['passport'] && $MOD['passport'] != 'uc') {
			$api_url = '';
			$user['password'] = Utils::isMd5($password) ? $password : md5($password);//Once MD5
			if(strtoupper($MOD['passport_charset']) != strtoupper(DT_CHARSET)) $user = Str::convert($user, DT_CHARSET, $MOD['passport_charset']);
			extract($user);
			include DT_ROOT.'/api/'.$MOD['passport'].'.inc.php';
			if($api_url) $forward = $api_url;
		}
		if($MOD['sso']) include DT_ROOT.'/api/sso.inc.php';
		if($DT['login_log'] == 2) $do->login_log($username, $password, 0);
		if($api_msg) Prompt::message($api_msg, $forward, -1);
		Prompt::message($api_msg, $forward);
	} else {
		if($DT['login_log'] == 2) $do->login_log($username, $password, 0, $do->errmsg);
		Prompt::message($do->errmsg);
	}
} else {
	isset($username) or $username = $_username;
	isset($password) or $password = '';
	$register = isset($register) && $username ? 1 : 0;
	if(!$username) $username = Cookie::get('username');
	if(!User::checkname($username)) $username = '';
	$OAUTH = PCache::read('oauth.php');
	$oa = 0;
	foreach($OAUTH as $v) {
		if($v['enable']) {
			$oa = 1;
			break;
		}
	}
	Cookie::set('forward_url', $forward);
	$head_title = $register ? $L['login_title_reg'] : $L['login_title'];
	include template('login', $module);
}
?>